/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.odichy.andfml.content;

import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.odichy.andfml.domain.Account;
import org.odichy.andfml.sax.AccountHandler;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

/**
 * AndFML, an FMyLife.com client on Google Android platform.
 *
 * @author Odin Liu<odinushuaia@gmail.com>
 *
 * @version 1.0.0
 */
public class AccountFactory {
	public static final String SIGNUP = "signup";
	public static final String LOGIN = "login";
	public static final String LOGOUT = "logout";
	public static final String INFOS = "infos";
	public static final String FAVOURITES_ADD = "favorites/add";
	public static final String FAVOURITES_DELETE = "favorites/delete";
	
	private String url;
	private Account account;
	private String message;
	
	public AccountFactory(String baseURL, String username, String password, String key, String language) {
		StringBuilder sb = new StringBuilder();
		sb.append(baseURL);
		sb.append("/");
		sb.append(LOGIN);
		sb.append("/");
		sb.append(username);
		sb.append("/");
		sb.append(password);
		this.url = sb.toString();
		login(key, language);
	}
	
	public AccountFactory(String baseURL, String mail, String username, String password, String key, String language) {
		StringBuilder sb = new StringBuilder();
		sb.append(baseURL);
		sb.append("/");
		sb.append(SIGNUP);
		sb.append("?mail=");
		sb.append(mail);
		sb.append("&login=");
		sb.append(username);
		sb.append("&pass=");
		sb.append(password);
		sb.append("&key=");
		sb.append(key);
		sb.append("&language=");
		sb.append(language);
		this.url = sb.toString();
		signup();
	}
	
	public AccountFactory(String baseURL, String token, String key, String language) {
		StringBuilder sb = new StringBuilder();
		sb.append(baseURL);
		sb.append("/");
		sb.append(LOGOUT);
		sb.append("/");
		sb.append(token);
		sb.append("?key=");
		sb.append(key);
		sb.append("&language=");
		sb.append(language);
		this.url = sb.toString();
		logout();
	}
	
	private void login(String key, String language) {
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			XMLReader xmlReader = parser.getXMLReader();
			AccountHandler handler = new AccountHandler();
			xmlReader.setContentHandler(handler);
			
			HttpPost post = new HttpPost(this.url);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("key", key));
			params.add(new BasicNameValuePair("language", language));
			post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			HttpResponse response = new DefaultHttpClient().execute(post);
			String result = EntityUtils.toString(response.getEntity());
			
			xmlReader.parse(new InputSource(new StringReader(result)));
			this.account = handler.getAccount();
		}
		catch(Exception e) {
			this.account = null;
			message = e.toString();
		}
	}
	
	private void signup() {
		try {
			URL ourl = new URL(this.url);
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			XMLReader xmlReader = parser.getXMLReader();
			AccountHandler handler = new AccountHandler();
			xmlReader.setContentHandler(handler);
			InputSource is = new InputSource(ourl.openStream());
			xmlReader.parse(is);
			this.account = handler.getAccount();
		}
		catch(Exception e) {
			this.account = null;
			message = e.toString();
		}
	}
	
	private void logout() {
		try {
			URL ourl = new URL(this.url);
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			XMLReader xmlReader = parser.getXMLReader();
			AccountHandler handler = new AccountHandler();
			xmlReader.setContentHandler(handler);
			InputSource is = new InputSource(ourl.openStream());
			xmlReader.parse(is);
			this.account = handler.getAccount();
		}
		catch(Exception e) {
			this.account = null;
			message = e.toString();
		}
	}

	public Account getAccount() {
		return account;
	}

	public String getMessage() {
		return message;
	}
}
